<!DOCTYPE html>
<html lang="en"  xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<script src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
<script src="js/jsencrypt.min.js"></script>

<body>
<h1 th:text="${msg}">RSA测试</h1>
<form>
    用户账号：
    <input type="text" name="username" id="username">
    <br>
    用户密码：
    <input type="text" name="password" id="password">
    <br>
    <input type="button" th:onclick="login()" th:value="提交">
</form>
<hr/>
<div id="div" th:style="'float:left; width:800px; height:500px; border-style:solid;border-width:1px;'"></div>
</body>
<script>
    function login() {
        var username = $('#username').val();
        var password = $('#password').val();
        var uncrypted;
        var encrypt = new JSEncrypt();
        var decrypt = new JSEncrypt();
        $.ajax({
            type: "get",  //提交方式
            url: "/getPublicKey",//访问路径
            contentType: 'application/json;charset=utf-8',//返回json结果
            success: function (data) {
                console.log(data)
                encrypt.setPublicKey(data)
                var encryptPwd = encrypt.encrypt(password)
                console.log("encryptPwd:"+encryptPwd)
                $.ajax({
                    type: "post",  //提交方式
                    url: "/loginRequest",//访问路径
                    contentType: 'application/json;charset=utf-8',//返回json结果
                    data: JSON.stringify({"username":username,"password":encryptPwd}),
                    success: function (data) {
                        console.log(data)
                        var json = eval(data);
                        var addr = json.addr;


                        $.ajax({
                            type: "get",
                            url: "/getPrivateKey",
                            contentType: "application/json;charset=utf-8",
                            success: function (data) {
                                // console.log("私钥%o:" ,data);
                                decrypt.setPrivateKey(data);
                                uncrypted = decrypt.decrypt(addr);
                                json.addr = uncrypted
                                console.log('解密后数据:%o', json);
                                div = document.getElementById("div");
                                // div.innerText = json.toString();
                                div.innerHTML =JSON.stringify(json);
                            }
                        });
                    }
                });
            }
        });

    }
</script>
</html>